package com.web.dgfight.client;

import java.util.LinkedList;
import java.util.Queue;

import com.google.gwt.user.client.Timer;

public class EventTimer {
	
	public int currentHealth;
	public int statEndurance;
	public Timer eventTimer;
	public int REFRESH_INTERVAL;
	static Queue<Integer> attackQueue = new LinkedList<Integer>();
	
	// the Bicycle class has one constructor
		public EventTimer(int currentHealth) {
						
		}
		
	// the start the game
	public void startEventTimer() {
		Timer eventTimer = new Timer() {
		      @Override
		      public void run() {
		    	  		    	  
		    	  int currentAttack;
		    	  if (currentHealth >0){
		    	  if (attackQueue.peek() == null){
		    			  currentAttack = 0;
		    		  }
		    		  else {
		    			  currentAttack = attackQueue.poll(); // get the head value from the attack queue
		    		  }
		    		  //to stop the timer looping with null values but ready of attacks are added
		    		  DGFightWeb.lblHealthScore.setText(Integer.toString(currentHealth));
		    		  DGFightWeb.lblAttackScore.setText(Integer.toString(currentAttack)); 	    		  
		    		  //moves the attack score up the list
		    		  final Integer[] attackQueueArray = attackQueue.toArray(new Integer[0]);
		    		  for (int i = 0; i < attackQueue.size(); i++){
		    			  DGFightWeb.attackListGrid.setText(i, 0, attackQueueArray[i].toString());
		    		  }
		    	  
		    		  int hRValue = (100 - currentHealth)/5;
		    		  currentHealth = currentHealth - currentAttack + hRValue; // calculate the new current health
		    		  DGFightWeb.lblTestText.setText(Integer.toString(hRValue));
		    		  for (int i = attackQueue.size(); i < 4; i++){
		    			  DGFightWeb.attackListGrid.setText(i, 0, " ");
		    		  }
		    	  }
		    	  else{DGFightWeb.lblHealthScore.setText("Winner");}
		      }
		};
		    eventTimer.scheduleRepeating(REFRESH_INTERVAL);
			
	}
	// end the game
	public void endEventTimer(){
		eventTimer.cancel();
	}

}

